/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.parameter_name;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MethodParameterRegistry {
	private final Map<String, String[]> methodParameters = new HashMap<String, String[]>();

	public void register(String className, String methodName, String[] parameterNames) {
		methodParameters.put(className + "." + methodName, parameterNames);
	}

	public String[] get(Constructor constructor) {
		return replaceNullArray(get(constructor.getDeclaringClass().getName(), "<init>"), constructor);
	}

	public String[] get(Method method) {
		return replaceNullArray(get(method.getDeclaringClass().getName(), method.getName()), method);
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Implementation
	///////////////////////////////////////////////////////////////////////////////////////////////

	private String[] get(String className, String methodName) {
		return methodParameters.get(className + "." + methodName);
	}

	private String[] replaceNullArray(String[] parameterNames, Constructor constructor) {
		return parameterNames != null ? parameterNames : new String[constructor.getParameterTypes().length];
	}

	private String[] replaceNullArray(String[] parameterNames, Method method) {
		return parameterNames != null ? parameterNames : new String[method.getParameterTypes().length];
	}
}
